/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import java.awt.Color;
import java.awt.Image;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

/**
 *
 * @author Sam
 */
public class InfoPanel extends javax.swing.JPanel {

 private static String OS = System.getProperty("os.name").toLowerCase();
    public InfoPanel() {
        initComponents();
        this.setVisible(true);
        String username = System.getProperty("user.name");
		java.net.InetAddress localMachine = null;
		try {
			localMachine = java.net.InetAddress.getLocalHost();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
                computer.setText(localMachine.getHostName());
                user.setText(username + " ( "+System.getProperty("user.home")+" )");
                ip.setText(getIp());
                String OS1 = System.getProperty("os.name").toLowerCase();
		 String OS2 = System.getProperty("os.arch").toLowerCase();
                 system.setText(OS1+" - "+OS2);
                 if (isWindows()) {
                     ImageIcon icon0= new javax.swing.ImageIcon(getClass().getResource("/res/windows.png"));
        Image img0 = icon0.getImage();
	Image newimg0 = img0.getScaledInstance(30, 30, java.awt.Image.SCALE_SMOOTH);
	ImageIcon iconres0 = new ImageIcon(newimg0);
			system.setIcon(iconres0);
		} else if (isMac()) {
			ImageIcon icon1= new javax.swing.ImageIcon(getClass().getResource("/res/apple.png"));
        Image img1 = icon1.getImage();
	Image newimg1 = img1.getScaledInstance(30, 30, java.awt.Image.SCALE_SMOOTH);
	ImageIcon iconres1 = new ImageIcon(newimg1);
			system.setIcon(iconres1);
		} else if (isUnix()) {
			ImageIcon icon1= new javax.swing.ImageIcon(getClass().getResource("/res/linux.png"));
        Image img1 = icon1.getImage();
	Image newimg1 = img1.getScaledInstance(30, 30, java.awt.Image.SCALE_SMOOTH);
	ImageIcon iconres1 = new ImageIcon(newimg1);
			system.setIcon(iconres1);
		} else if (isSolaris()) {
			ImageIcon icon1= new javax.swing.ImageIcon(getClass().getResource("/res/linux.png"));
        Image img1 = icon1.getImage();
	Image newimg1 = img1.getScaledInstance(30, 30, java.awt.Image.SCALE_SMOOTH);
	ImageIcon iconres1 = new ImageIcon(newimg1);
			system.setIcon(iconres1);
		} else {
		}
    }
    public static boolean isWindows() {
 
		return (OS.indexOf("win") >= 0);
 
	}
 
	public static boolean isMac() {
 
		return (OS.indexOf("mac") >= 0);
 
	}
 
	public static boolean isUnix() {
 
		return (OS.indexOf("nix") >= 0 || OS.indexOf("nux") >= 0 || OS.indexOf("aix") > 0 );
 
	}
 
	public static boolean isSolaris() {
 
		return (OS.indexOf("sunos") >= 0);
 
	}
    public static String getIp() {
        String ip = "127.0.0.1";
        try {
            URL whatismyip = new URL("http://checkip.amazonaws.com");
            BufferedReader in = null;
            try {
                try {
                    in = new BufferedReader(new InputStreamReader(
                            whatismyip.openStream()));
                } catch (IOException ex) {
                    Logger.getLogger(InfoPanel.class.getName()).log(Level.SEVERE, null, ex);
                    
                }
                if(in!= null){
                 ip = in.readLine();
                 in.close();
                }
                else{ip = "Unknown";}
                return ip;
                
            } catch (IOException ex) {
                Logger.getLogger(InfoPanel.class.getName()).log(Level.SEVERE, null, ex);
                try {
                    in.close();
                } catch (IOException ex1) {
                    Logger.getLogger(InfoPanel.class.getName()).log(Level.SEVERE, null, ex1);
                }
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        } catch (MalformedURLException ex) {
            Logger.getLogger(InfoPanel.class.getName()).log(Level.SEVERE, null, ex);
            return ip;
                    
        }
        return ip;
    }
    public void setPort(String port){
        getPort().setText(port);
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jTabbedPane1 = new javax.swing.JTabbedPane();
      //  this = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        computer = new javax.swing.JLabel();
        user = new javax.swing.JLabel();
        ip = new javax.swing.JLabel();
        system = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        portLabel = new javax.swing.JLabel();
        timeLabel = new javax.swing.JLabel();


        this.setLayout(new java.awt.GridLayout(0, 1));

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "My Computer :", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, Color.GREEN));
        jPanel4.setMinimumSize(new java.awt.Dimension(0, 0));
        jPanel4.setPreferredSize(new java.awt.Dimension(441, 100));
        jPanel4.setLayout(new java.awt.GridBagLayout());
        jPanel4.add(jScrollPane1, new java.awt.GridBagConstraints());

        jLabel1.setText("Computer Name :");
        jLabel1.setOpaque(true);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(6, 31, 0, 0);
        jPanel4.add(jLabel1, gridBagConstraints);

        jLabel2.setText("User Name :");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(6, 31, 0, 0);
        jPanel4.add(jLabel2, gridBagConstraints);

        jLabel3.setText("IP Adress :");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(6, 31, 0, 0);
        jPanel4.add(jLabel3, gridBagConstraints);

        jLabel4.setText("Operating System :");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(6, 31, 0, 0);
        jPanel4.add(jLabel4, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weightx = 1.0;
        jPanel4.add(computer, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weightx = 1.0;
        jPanel4.add(user, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weightx = 1.0;
        jPanel4.add(ip, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weightx = 1.0;
        jPanel4.add(system, gridBagConstraints);

        this.add(jPanel4);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "RatRoid Infos :", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, Color.GREEN));
        jPanel2.setPreferredSize(new java.awt.Dimension(441, 100));
        jPanel2.setLayout(new java.awt.GridBagLayout());

        jLabel5.setText("Listening on port :");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(6, 31, 0, 0);
        jPanel2.add(jLabel5, gridBagConstraints);

        jLabel6.setText("RatRoid version :");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(6, 31, 0, 0);
        jPanel2.add(jLabel6, gridBagConstraints);

        jLabel7.setText("Server Started :");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(6, 31, 0, 0);
        jPanel2.add(jLabel7, gridBagConstraints);

        

        jLabel9.setText("1.0");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weightx = 1.0;
        jPanel2.add(jLabel9, gridBagConstraints);

        portLabel.setText("jLabel10");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weightx = 1.0;
        jPanel2.add(portLabel, gridBagConstraints);

        timeLabel.setText("---");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weightx = 1.0;
        jPanel2.add(timeLabel, gridBagConstraints);



        this.add(jPanel2);


    }
    
    public JLabel getPort(){return portLabel;}
    public JLabel getTime(){return timeLabel;}
    
    private javax.swing.JLabel computer;
    private javax.swing.JLabel ip;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel portLabel;
    private javax.swing.JLabel timeLabel;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JLabel system;
    private javax.swing.JLabel user;
  }
